package org.lzh.controller;

import org.lzh.pojo.Answer;
import org.lzh.pojo.Problem;
import org.lzh.service.AnswerService;
import org.lzh.service.TestPaperAndProblemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Currency;
import java.util.Date;
import java.util.List;


//用于前端的控制
@Controller
@RequestMapping("surface")
public class SurfaceController {

    @Autowired
    private TestPaperAndProblemService testPaperAndProblemService;

    @Autowired
    private AnswerService answerService;            //用于试卷进去的时候插入答案 state=0，时间设为当前时间


    //进入考试界面
    @RequestMapping(value="/entryTest")
    public String hhh(Model model,
                      String testPaperId,
                      String studentId
                      ){


        //如果答案的数据库里该同学已经做过这试卷了就不插入
        if(answerService.findByStudentId(studentId)==null){
//插入答案，state=0，startTime=当前时间
            Answer answer=new Answer();
            answer.setState(0);
            answer.setStartTime(new Date());
            answer.setStudentId(studentId);
            answer.setTestPaperId(testPaperId);
//        System.out.println(studentId);
            answerService.insertAnswer(answer);


            //于是根据选的试卷进行考试
            List<Problem> allProblem=testPaperAndProblemService.findAllProblem(testPaperId);
            model.addAttribute("studentList",allProblem);
            //还需要传一个试卷的id
            model.addAttribute("testPaperId",testPaperId);
            //还需要传一个学生的id
//        model.addAttribute("user_session",session);

        }else{
            //如果答案里已经有了，不管点哪个试卷都进入答案里的试卷
            List<Problem> allProblem=testPaperAndProblemService.findAllProblem(answerService.findByStudentId(studentId).getTestPaperId());
            model.addAttribute("studentList",allProblem);
            //还需要传一个试卷的id
            model.addAttribute("testPaperId",testPaperId);
            //还需要传一个学生的id
//        model.addAttribute("user_session",session);

        }

        //通过学生id查找出答案的开始时间
        Date startTime=answerService.findByStudentId(studentId).getStartTime();
        //获取startTime，要转成"yyyy-MM-dd HH:mm:ss"格式是因为直接传给页面会乱码
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String startTime1=simpleDateFormat.format(startTime);
        model.addAttribute("startTime",startTime1);
        System.out.println(startTime1);

        return "testPaper";

    }

    //进入index.html
    @RequestMapping(value="/index1")
    public String h(Model model){

        return "student";

    }



}
